package com.fsai.cms.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fsai.cms.base.WebCons;
import com.fsai.cms.config.CmsSiteConfig;
import com.fsai.cms.domain.BaseOrg;
import com.fsai.cms.domain.BaseSiteCfg;
import com.fsai.cms.domain.CmsCarouselPic;
import com.fsai.cms.domain.CmsCate;
import com.fsai.cms.domain.CmsCont;
import com.fsai.cms.domain.CmsFriendlyLinks;
import com.fsai.cms.domain.CmsIndexBanner;
import com.fsai.cms.domain.CmsSpread;
import com.fsai.cms.service.ICmsCateService;
import com.fsai.cms.service.ICmsContService;
import com.fsai.cms.service.ICmsPubService;

/**
 *
 * @project: cms-index
 *
 * @author: JaffeHe
 * @email: JaffeHe@foxmail.com
 *
 * @ChangeLog<br/>
 * 				Developer Date Comment <br/>
 */
@Controller
@RequestMapping
public class IndexController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ICmsCateService cmsCateService;
	@Autowired
	private ICmsContService cmsContService;
	@Autowired
	private ICmsPubService cmsPubService;
	@Autowired
	private CmsSiteConfig siteCfg;

	//取出主页进行重定向
	@RequestMapping("")
	public String def(ModelMap mmap) {
		//查询站点信息
		BaseSiteCfg siteInfo = cmsPubService.selSiteInfo(siteCfg.getMainId());

		//1如果维护升级
		if (WebCons.STATUS_N.equals(siteInfo.getVisitEnable())) {
			//重定向到维护页面 - 转发
			return "forward:/page/repair";
		}
		//2正常情况重定向配置主页
		return "redirect:/" + siteInfo.getIndexPage();
	}

	//主站主页
	@RequestMapping("/index")
	public String index(ModelMap mmap) {
		//取出站点信息
		BaseSiteCfg siteInfo = cmsPubService.selSiteInfo(siteCfg.getMainId());
		mmap.put("siteInfo", siteInfo);
		//网站轮播图
		String orgCode = cmsPubService.getSycConfVal("biz.deforg.code");
		List<CmsCarouselPic> carouselPics = cmsPubService.findOrgCarouselPics(orgCode);
		mmap.put("carouselPics", carouselPics);

		//横幅图片
		CmsIndexBanner indexBanner = cmsPubService.findIndexBanner();
		mmap.put("indexBanner", indexBanner);

		//内设机构
		List<BaseOrg> showSubOrgList = new ArrayList<>();
		List<BaseOrg> subOrgList = cmsPubService.findSubOrg();
		String hideOrgCodes = cmsPubService.getSycConfVal("biz.hide.orgcodes");
		for (BaseOrg baseOrg : subOrgList) {
			if (hideOrgCodes.indexOf(baseOrg.getOrgCode()) < 0) {
				showSubOrgList.add(baseOrg);
			}
		}
		mmap.put("subOrgList", showSubOrgList);

		//传播平台
		List<CmsSpread> spreads = cmsPubService.selectSpreads();
		mmap.put("spreads", spreads);

		//友情链接
		List<CmsFriendlyLinks> friendLinks = cmsPubService.selectFriendLinks();
		mmap.put("friendLinks", friendLinks);

		//中心动态为信息公开和新闻动态的最新文章+内设机构同步的文章
		int countChar = Integer.parseInt(cmsPubService.getSycConfVal("biz.count.char"));
		mmap.put("countChar", countChar);

		int showCount = Integer.parseInt(cmsPubService.getSycConfVal("biz.show.indexcount"));
		String showCateIds = cmsPubService.getSycConfVal("biz.show.cateids");
		List<CmsCont> centerConts = cmsContService.queryIndexCenterConts(showCateIds, showCount);
		mmap.put("centerConts", centerConts);

		//国务院信息链接
		String linkGwy = cmsPubService.getSycConfVal("biz.link.gwy");
		mmap.put("linkGwy", linkGwy);
		//国家卫健委链接
		String linkWjw = cmsPubService.getSycConfVal("biz.link.wjw");
		mmap.put("linkWjw", linkWjw);

		//首页党建文化最新新闻1
		String cateCode1 = cmsPubService.getSycConfVal("biz.code.cateone");
		CmsCate cmsCate1 = cmsCateService.queryCateByCode(cateCode1);
		List<CmsCont> news1 = cmsContService.selectTopNews(cmsCate1.getCateId(), cmsCate1.getRecomPageCount());
		mmap.put("news1", news1);
		mmap.put("cmsCate1", cmsCate1);
		//首页显示列表2
		String cateCode2 = cmsPubService.getSycConfVal("biz.code.catetwo");
		CmsCate cmsCate2 = cmsCateService.queryCateByCode(cateCode2);
		List<CmsCont> news2 = cmsContService.selectTopNews(cmsCate2.getCateId(), cmsCate2.getRecomPageCount());
		mmap.put("news2", news2);
		mmap.put("cmsCate2", cmsCate2);

		return "pcdc/index";
	}

	//头部
	@RequestMapping("/head")
	public String head(ModelMap mmap) {
		//0取出参数配置主机构代码
		String orgCode = cmsPubService.getSycConfVal("biz.deforg.code");
		int topCateId = cmsPubService.findOrgTopCateId(orgCode);
		//1取出主站所有栏目
		List<CmsCate> cateList = cmsCateService.selectCateListToTree(orgCode, topCateId);
		mmap.put("cateList", cateList);
		//2.获取站点信息
		BaseSiteCfg siteInfo = cmsPubService.selSiteInfo(siteCfg.getMainId());
		mmap.put("siteInfo", siteInfo);
		return "pcdc/head";
	}

	//底部
	@RequestMapping("/foot")
	public String foot(ModelMap mmap) {
		return "pcdc/foot";
	}

	//友情链接
	@RequestMapping("/flink")
	public String friendLink(ModelMap mmap) {
		//友情链接
		List<CmsFriendlyLinks> friendLinks = cmsPubService.selectFriendLinks();
		mmap.put("friendLinks", friendLinks);

		return "pcdc/flink";
	}

	//子站点主页
	@RequestMapping("/index/{orgCode}")
	public String subIndex(@PathVariable("orgCode") String orgCode, ModelMap mmap) {
		//查询站点信息
		BaseSiteCfg siteInfo = cmsPubService.selSiteInfo(siteCfg.getMainId());

		//1如果维护升级
		if (WebCons.STATUS_N.equals(siteInfo.getVisitEnable())) {
			//重定向到维护页面 - 转发
			return "forward:/page/repair";
		}

		mmap.put("orgCode", orgCode);
		//文章列表文字数量
		int countChar = Integer.parseInt(cmsPubService.getSycConfVal("biz.count.char"));
		mmap.put("countChar", countChar);
		//取出站点信息
		mmap.put("siteInfo", siteInfo);
		//网站轮播图
		List<CmsCarouselPic> carouselPics = cmsPubService.findOrgCarouselPics(orgCode);
		mmap.put("carouselPics", carouselPics);

		//横幅图片
		CmsIndexBanner indexBanner = cmsPubService.findIndexBanner();
		mmap.put("indexBanner", indexBanner);

		//机构信息
		BaseOrg org = cmsPubService.findOrgInfoByCode(orgCode);
		mmap.put("org", org);

		//子机构文章列表1
		String cateCode1 = cmsPubService.getSycConfVal("biz.code." + orgCode + ".cateone");
		CmsCate cmsCate1 = cmsCateService.queryCateByCode(cateCode1);
		List<CmsCont> news1 = cmsContService.selectTopNews(cmsCate1.getCateId(), cmsCate1.getRecomPageCount());
		mmap.put("news1", news1);
		mmap.put("cmsCate1", cmsCate1);
		//首页显示列表2
		String cateCode2 = cmsPubService.getSycConfVal("biz.code." + orgCode + ".catetwo");
		CmsCate cmsCate2 = cmsCateService.queryCateByCode(cateCode2);
		List<CmsCont> news2 = cmsContService.selectTopNews(cmsCate2.getCateId(), cmsCate2.getRecomPageCount());
		mmap.put("news2", news2);
		mmap.put("cmsCate2", cmsCate2);

		return "pcdc/subindex";
	}

	//子站点头部
	@RequestMapping("/head/{orgCode}")
	public String subhead(@PathVariable("orgCode") String orgCode, ModelMap mmap) {
		//0取出参数配置主机构代码
		int topCateId = cmsPubService.findOrgTopCateId(orgCode);
		//1取出主站所有栏目
		List<CmsCate> cateList = cmsCateService.selectCateListToTree(orgCode, topCateId);
		mmap.put("cateList", cateList);
		//2.获取站点信息
		BaseSiteCfg siteInfo = cmsPubService.selSiteInfo(siteCfg.getMainId());
		mmap.put("siteInfo", siteInfo);
		//3机构信息
		BaseOrg org = cmsPubService.findOrgInfoByCode(orgCode);
		mmap.put("org", org);
		mmap.put("orgCode", orgCode);

		//4当前年月日

		return "pcdc/subhead";
	}

}
